package com.zj.leetcode.medium;

/**
 * @program: algorithm
 * @description:
 * 299. 猜数字游戏
 * @author: Zhang Bo
 * @create: 2022-01-13 15:12
 **/
public class BullsAndCows {
    public static void main(String[] args) {
//        String secret = "1807", guess = "7810";
//        String secret = "1123", guess = "0111";
//        String secret = "1", guess = "0";
        String secret = "1122", guess = "1222";
        Solution solution = new BullsAndCows().new Solution();
        System.out.println(solution.getHint(secret, guess));
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {

        public String getHint(String secret, String guess) {
            //公牛
            int a = 0;
            //母牛
            int b = 0;

            int[] sBarrel = new int[10];
            int[] gBarrel = new int[10];


            for (int i = 0; i < secret.length(); i++) {
                if (guess.charAt(i) == secret.charAt(i)) {
                    ++a;
                } else {
                    ++gBarrel[guess.charAt(i) - 48];
                    ++sBarrel[secret.charAt(i) - 48];
                }

            }

            for (int i = 0; i < secret.length(); i++) {
                int num = guess.charAt(i) - 48;
                if (secret.contains(String.valueOf(guess.charAt(i)))) {
                    if (sBarrel[num] > 0 && gBarrel[num] > 0) {
                        ++b;
                        --sBarrel[num];
                        --gBarrel[num];
                    }
                }
            }

            return a + "A" + b + "B";
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
